package com.tn.controller.vehicle;

import cn.hutool.core.collection.ListUtil;
import com.gitee.hifong45.FE;
import com.gitee.hifong45.Msg;
import com.gitee.hifong45.Ret;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tn.controller.alarm.common.constants.Constants;
import com.tn.controller.alarm.common.model.CommonVO;
import com.tn.controller.alarm.common.model.PageInput;
import com.tn.controller.alarm.common.model.PageResult;
import com.tn.controller.alarm.common.model.vehicle.entity.VehicleInfoDO;
import com.tn.controller.alarm.common.model.vehicle.vo.VehicleInParam;
import com.tn.controller.alarm.common.model.vehicle.vo.VehicleTaskInfoVO;
import com.tn.controller.alarm.common.model.vehicle.vo.VehicleWorkWeightVO;
import com.tn.dao.mbpdao.TaskInfo;
import com.tn.service.vehicle.IVehicleService;
import com.tn.service.vehicle.impl.VehicleServiceImpl;
import com.tn.utils.CheckParamUtil;
import com.tn.vo.SubTaskVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 装载车controller
 *
 * @program: tn_aucy_java
 * @author: yangjiayao
 * @create: 2022/03/01
 */
@Slf4j
@RestController
@RequestMapping("/aucy/vehicle")
public class VehicleController {
    /**
     * 装载车service
     */
    private IVehicleService vehicleService;

    /**
     * 自动注入
     *
     * @param vehicleService vehicleService
     */
    @Autowired
    public void setVehicleService(IVehicleService vehicleService) {
        this.vehicleService = vehicleService;
    }

    @Resource
    private VehicleServiceImpl vehicleServiceImpl;

    /**
     * 是否已绑定装载车
     *
     * @param param 入参
     * @return 是否绑定
     */
    @RequestMapping("/isBoundDriver")
    public CommonVO<VehicleInfoDO> isBoundDriver(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkBlankRequiredParam(param.getDriver());
        return new CommonVO<>(vehicleService.isBoundDriver(param.getDriver()));
    }



    /**
     * 更改装载车任务状态：接收/停止态（默认）、启动、放弃、完成
     * @param license
     * @param taskNo
     * @return
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "更改装载车任务状态")
    @GetMapping("/task/handle")
    public Ret<Object> handleTask(String license, Integer taskId, String taskNo, Integer type) {
        FE.nullThrow(license, "license");
        FE.nullThrow(taskId, "taskId");
        if(type == null || type == 0) {
            type = Constants.STATUS_VALID;
        }
        // 允许修改的状态
        var validTypes = ListUtil.of(Constants.STATUS_VALID, Constants.STATUS_START,
                Constants.STATUS_CANCEL, Constants.STATUS_PAUSE);
        FE.trueThrow(!validTypes.contains(type), Msg.ERROR_PARAMETER, " type cann't be: " + type);

        vehicleServiceImpl.handleTask(license, taskId, taskNo, type);
        return Ret.Ok();
    }

    /**
     * 查询总任务的子任务列表
     * @param taskNo 总任务号
     * @return
     */
    @ApiOperation(value = "查询总任务的子任务列表")
    @GetMapping("/subtask")
    public Ret<List<SubTaskVo>> getSubTask(String taskNo) {
        List<SubTaskVo> ret = vehicleServiceImpl.getSubTask(taskNo);
        return Ret.Ok(ret);
    }

    /**
     * 更改子任务：开始、暂停、完成
     * @param subTaskId 子任务ID
     * @return
     */
    @ApiOperation(value = "更改子任务状态")
    @GetMapping("/subtask/handle")
    public Ret<List<SubTaskVo>> handleSubTask(String subTaskId, Integer type, String vehicleNo) {
        vehicleServiceImpl.handleSubTask(subTaskId, type, vehicleNo);
        return Ret.Ok();
    }

    @ApiOperation(value = "读取装载车和货车匹配信息")
    @GetMapping("/trucks")
    public Ret<List<SubTaskVo>> getMatchTruck(String vehicleNo) {
        var ret = vehicleServiceImpl.getMatchTruck(vehicleNo);
        return Ret.Ok(ret);
    }


    /**
     * 查询空闲的装载车
     *
     * @return 空闲装载车列表
     */
    @RequestMapping(value = "/queryInactiveVehicle", method = RequestMethod.GET)
    public CommonVO<List<VehicleInfoDO>> queryInactiveVehicle() {
        return new CommonVO<>(vehicleService.queryInactiveVehicle());
    }

    /**
     * 绑定装载车和司机
     *
     * @param param 入参
     * @return 绑定结果
     */
    @RequestMapping("/bindingDriver")
    public CommonVO<Object> bindingDriver(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkNullRequiredParam(param.getVehicleId());
        CheckParamUtil.checkBlankRequiredParam(param.getDriver());
        vehicleService.boundDriver(param.getVehicleId(), param.getDriver());
        return new CommonVO<>();
    }

    /**
     * 查询装载车理货任务信息
     *
     * @param param 入参
     * @return 装载车理货任务信息
     */
    @RequestMapping("/queryTaskInfo")
    public CommonVO<List<VehicleTaskInfoVO>> queryTaskInfo(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkNullRequiredParam(param.getVehicleId());
        return new CommonVO<>(vehicleService.queryVehicleTaskInfo(param.getVehicleId()));
    }

    /**
     * 装载车下发
     *
     * @param param 入参
     * @return 下发结果
     */
    @RequestMapping("/sendCall")
    public CommonVO<VehicleTaskInfoVO> sendCall(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkNullRequiredParam(param.getTaskId(), param.getVehicleId());
        CheckParamUtil.checkBlankRequiredParam(param.getVehicleLicense());
        return new CommonVO<>(vehicleService.sendCall(param.getTaskId(), param.getVehicleId(),
                param.getVehicleLicense()));
    }

    /**
     * 装载车下发
     *
     * @param param 入参
     * @return 下发结果
     */
    @RequestMapping("/cancelCall")
    public CommonVO<VehicleTaskInfoVO> cancelCall(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkNullRequiredParam(param.getTaskId());
        CheckParamUtil.checkBlankRequiredParam(param.getVehicleLicense());
        return new CommonVO<>(vehicleService.cancelCall(param.getTaskId(), param.getVehicleLicense()));
    }

    /**
     * 装载车已匹配
     *
     * @param param 入参
     * @return 下发结果
     */
    @RequestMapping("/matched")
    public CommonVO<VehicleTaskInfoVO> matched(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkNullRequiredParam(param.getTaskId());
        return new CommonVO<>(vehicleService.matched(param.getTaskId()));
    }

    /**
     * 装载车暂停理货
     *
     * @param param 入参
     * @return 下发结果
     */
    @RequestMapping("/pausedTask")
    public CommonVO<VehicleTaskInfoVO> pausedTask(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkNullRequiredParam(param.getTaskId());
        return new CommonVO<>(vehicleService.pausedTask(param.getTaskId()));
    }

    /**
     * 装载车继续理货
     *
     * @param param 入参
     * @return 下发结果
     */
    @RequestMapping("/continueTask")
    public CommonVO<VehicleTaskInfoVO> continueTask(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkNullRequiredParam(param.getTaskId());
        return new CommonVO<>(vehicleService.continueTask(param.getTaskId()));
    }

    /**
     * 装载车完成理货
     *
     * @param param 入参
     * @return 下发结果
     */
    @RequestMapping("/completeTask")
    public CommonVO<VehicleTaskInfoVO> completeTask(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkNullRequiredParam(param.getTaskId());
        vehicleService.completeTask(param.getTaskId());
        return new CommonVO<>();
    }

    /**
     * 查询装载车历史理货记录
     *
     * @param pageParam 分页参数
     * @return 装载车历史理货记录
     */
    @RequestMapping("/queryHisTaskInfo")
    public CommonVO<PageResult<VehicleTaskInfoVO>> queryHisTaskInfo(@RequestBody PageInput<VehicleInParam> pageParam) {
        CheckParamUtil.checkPageParam(pageParam.getCurPage(), pageParam.getPageSize());
        return new CommonVO<>(vehicleService.queryHisTaskInfo(pageParam));
    }

    /**
     * 查询装载车工作重量
     *
     * @param param 入参
     * @return 工作重量
     */
    @RequestMapping("/queryWorkWeight")
    public CommonVO<VehicleWorkWeightVO> queryWorkWeight(@RequestBody VehicleInParam param) {
        CheckParamUtil.checkNullRequiredParam(param.getVehicleId());
        return new CommonVO<>(vehicleService.queryWorkWeight(param.getVehicleId()));
    }
}
